package bold.deposit.mybatis;

import bold.deposit.interception.NoAuthorize;
import bold.deposit.mybatis.mapper.UserMapper;
import bold.deposit.mybatis.model.User;
import bold.deposit.useful.Request;
import bold.storage.component.BaseDao;
import bold.storage.core.Page;
import bold.storage.util.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

@Validated
@RestController
@RequestMapping("user")
@NoAuthorize
public class UserController extends BaseDao {

    @Autowired
    public UserMapper userMapper;

    @Autowired
    public UserService userService;

    /**
     * 比 mybatis 性能好一些
     */
    @RequestMapping("get")
    public User get(String id) throws SQLException {
        long start = System.currentTimeMillis();
        User user = userMapper.get(id);
        System.err.println(System.currentTimeMillis() - start);


        long start1 = System.currentTimeMillis();
        User record = query("user").where("id", id).entity(User.class);
        System.err.println(System.currentTimeMillis() - start1);

        return user;
    }

    @RequestMapping("save")
    public Object save(User user) {
        user.setId(ID.instance.getLong());
        user.setName("名字");
        user.setAge(88);
        return userMapper.save(user);
    }

    @RequestMapping("edit")
    public Integer edit(User user) {
        return userMapper.edit(user);
    }

    @RequestMapping("list")
    public List<User> list(Request request, Long[] id) {
        return userMapper.list(request.getPrams(), Arrays.asList(id));
    }

    @RequestMapping("page")
    public Page page(Request request) {
        return userService.page(request.getPrams());
    }
}
